package com.changgou.search.controller;

import com.changgou.search.feign.SkuEsFeign;
import com.changgou.search.pojo.SkuInfo;
import entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;
//rest是json数据类型；要页面跳转，所以就不返回json数据
@Controller
@RequestMapping(value = "/search")
public class SkuSearchController {

    @Autowired
    private SkuEsFeign skuEsFeign;

    @GetMapping(value="/list")
    public String search(@RequestParam(required = false) Map<String,String> searchMap, Model model){
        searchChar(searchMap);
//接收前端传过来的map<string,string>数据
        Map<String,Object> mapResult =  skuEsFeign.search(searchMap);
        //搜索的数据结果
        model.addAttribute("resultMap",mapResult);

        //分页数据,总记录数（total）当前页（pageNUm;要加1）每页显示的数据条数(pageSize);分页已经实现，用Page工具类可调节分页偏移量
        Page<SkuInfo> page = new Page<SkuInfo>(
                Long.parseLong(mapResult.get("total").toString()),
                Integer.parseInt(mapResult.get("pageNumber").toString()),
                Integer.parseInt(mapResult.get("pageSize").toString())
        );
model.addAttribute("pageInfo",page);

        //搜索的条件，用于回显
        model.addAttribute("searchMap",searchMap);

        String[] urls = url(searchMap); //调用url拼接函数
        model.addAttribute("surl",urls[0]);
        model.addAttribute("sortUrl",urls[1]);
        return "search";
    }

    /* 拼接url地址，完成条件点击搜索 */
    public String[] url(Map<String,String> searchMap){
       String url="/search/list"; // 初始化原始地址
        String sortUrl= "/search/list";

        if(searchMap!=null&&searchMap.size()>0){
            url+="?";
            sortUrl+="?";
        for (Map.Entry<String, String> entry : searchMap.entrySet()) {
                String key = entry.getKey();
                //跳过分页参数，无论什么操作都不能有分页参数，因为默认都是从第一页开始，前端的url分页会自己构建传送搜索相关结果
            if(key.equalsIgnoreCase("pageNum")){continue;}
                String value = entry.getValue();
                url+=key+"="+value+"&";
                //没有排序参数就记住sortUrl，有的话无须记住，因为排序条件只有一个；别写成与，不然满足不了条件
            if(key.equalsIgnoreCase("sortField")||key.equalsIgnoreCase("sortRule")){
                    continue;
            }
                sortUrl+=key+"="+value+"&";
        }
        url=url.substring(0,url.length()-1); //去掉最后的&
            sortUrl=sortUrl.substring(0,sortUrl.length()-1); //去掉最后的&
        }
        return new String[]{url,sortUrl};
    }

/*
替换特殊字符+
 */
public void searchChar(Map<String,String> searchMap){
     if(searchMap != null){
         for (Map.Entry<String,String> entry:searchMap.entrySet()){
             if(entry.getKey().startsWith("spec_")) {
                 entry.setValue(entry.getValue().replace("+", "%2B"));
             }
         }
     }
}

}
